﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DSAClient.Core;
using DSAClient.Models.Input;
using DSAClient.Models.Output;
using DSAClient.Properties;

namespace DSAClient
{
    public partial class GridForm : TemplateForm
    {
        Point mouseOff;//鼠标移动位置变量
        bool leftFlag;//标记是否为左键

        public GridForm()
        {
            InitializeComponent();
            dgOrder.AutoGenerateColumns = false;
        }

        public GridForm(List<TaobaoSunOrderInfo> tbsoiList)
        {
            InitializeComponent();
            dgOrder.AutoGenerateColumns = false;
            dgOrder.DataSource = tbsoiList;
        }

        private void titlePanel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Cursor = Cursors.Hand;
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                leftFlag = true;                  //点击左键按下时标注为true;
            }

        }

        private void titlePanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }

        private void titlePanel_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为false;
                this.Cursor = Cursors.Default;
            }
        }

        private void dgOrder_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Cursor = Cursors.Hand;
                mouseOff = new Point(-e.X, -e.Y - 123); //得到变量的值
                leftFlag = true;                  //点击左键按下时标注为true;
            }
        }

        private void dgOrder_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }

        private void dgOrder_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为false;
                this.Cursor = Cursors.Default;
            }
        }

        private void dgOrder_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 9)
            {
                var num = Convert.ToInt32(((List<TaobaoSunOrderInfo>)((DataGridView)(sender)).DataSource)[e.RowIndex].num);
                var size = ((List<TaobaoSunOrderInfo>)((DataGridView)(sender)).DataSource)[e.RowIndex].skupropertiesname;
                var name = ((List<TaobaoSunOrderInfo>)((DataGridView)(sender)).DataSource)[e.RowIndex].title;
                var no = ((List<TaobaoSunOrderInfo>)((DataGridView)(sender)).DataSource)[e.RowIndex].no;
                var oid = ((List<TaobaoSunOrderInfo>)((DataGridView)(sender)).DataSource)[e.RowIndex].oid;
                var producttype = ((List<TaobaoSunOrderInfo>)((DataGridView)(sender)).DataSource)[e.RowIndex].producttype;
                var picpaper = ((List<TaobaoSunOrderInfo>)((DataGridView)(sender)).DataSource)[e.RowIndex].picpaper;

                //获取图片尺寸列表
                var result = Api.QueryPhotoSize();
                PhotoSizeInfo psi = new PhotoSizeInfo();

                ImageForm imageForm = (ImageForm)OpenForm("ImageForm", new object[] { 0, num, size, producttype, name });
                imageForm.currpage = 1;
                imageForm.tempNum = 0;
                imageForm.isTxtChange = false;
                imageForm.lbSize.Text = size;
                imageForm.lbNum.Text = num.ToString() + "张";
                imageForm.lbName.Text = name;
                if (imageForm.sizeImageInfoList == null || imageForm.sizeImageInfoList.Count == 0)
                {
                    foreach (TaobaoSunOrderInfo tsoi in ((List<TaobaoSunOrderInfo>)((DataGridView)(sender)).DataSource))
                    {
                        if (result != null && result.data != null)
                        {
                            psi = result.data.Find((PhotoSizeInfo s) => s.name == tsoi.skupropertiesname && s.picturetype == tsoi.producttype);
                        }
                        var tempList = new List<ImageInfo>();
                        imageForm.sizeImageInfoList.Add(new SizeImageInfoList { no = tsoi.no, oid = tsoi.oid, producttype = tsoi.producttype, imgSize = new Size((int)psi.height, (int)psi.width), picpaper = tsoi.picpaper, size = tsoi.skupropertiesname, imageInfoList = tempList, ordernum = Convert.ToInt32(tsoi.num), surplusnum = Convert.ToInt32(tsoi.num), title = tsoi.title, totalnum = 0, errorfile = Guid.NewGuid().ToString(), iserror = false });
                    }
                }

                var temp = imageForm.sizeImageInfoList.Find((SizeImageInfoList s) => s.size == size && s.picpaper == picpaper && s.producttype == producttype);
                if (temp != null)
                {
                    imageForm.imageInfoList = temp.imageInfoList;
                    imageForm.sizeImageInfo = temp;
                    imageForm.imgSize = size;
                    imageForm.picturetype = producttype;
                }
                else
                {
                    var tempList = new List<ImageInfo>();
                    var sii = new SizeImageInfoList { no = no, oid = oid, producttype = producttype, imgSize = new Size((int)psi.height, (int)psi.width), picpaper = picpaper, size = size, imageInfoList = tempList, surplusnum = num, title = name, totalnum = 0, errorfile = Guid.NewGuid().ToString(), iserror = false };
                    imageForm.sizeImageInfoList.Add(sii);
                    imageForm.imageInfoList = tempList;
                    imageForm.sizeImageInfo = sii;
                }

                imageForm.Show();
                imageForm.sbtnorderlist.BackgroundImage = Resources.openorderlist;
                imageForm.ShowImage(currpage);
                this.Hide();
            }
        }

        private void dgOrder_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e == null || e.Value == null || !(sender is DataGridView))
                return;
            DataGridView view = (DataGridView)sender;
            object originalValue = e.Value;
            if (view.Columns[e.ColumnIndex].DataPropertyName == "status")
                e.Value = CommonHelper.GetEnumDes((OrderStatus)Enum.Parse(typeof(OrderStatus), e.Value.ToString())); //(OrderStatus)Enum.Parse(typeof(OrderStatus), e.Value);

        }
    }
}
